package com.liuguang.testassert.security;

import com.liuguang.testassert.dao.UserMapper;
import com.liuguang.testassert.pojo.Role;
import com.liuguang.testassert.vo.UserData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: MyUserDetailServiceImpl
 * @Description：
 * @Author: wanghr
 * Date: 9:39 AM 2020/7/1
 * Version: 1.0
 */
@Slf4j
@Component
public class MyUserDetailServiceImpl implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserData userData = userMapper.selectUserByUserName(username);
        List<Role> roleList = userData.getRole();
        log.info("进入MyUserDetailServiceImpl：角色列表：{}",roleList);
        ArrayList<GrantedAuthority> authories = new ArrayList<>();
        for (Role role : roleList) {
            authories.add(new SimpleGrantedAuthority(role.getRol()));
        }
        return new User(username,userData.getPassword(),authories);
    }
}
